home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / comm / simpcomm.frm < prev    next >
Text File  |  1995-05-08  |  18KB  |  500 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   4230
  7.    ClientLeft      =   1080
  8.    ClientTop       =   1815
  9.    ClientWidth     =   7365
  10.    Height          =   4635
  11.    Icon            =   SIMPCOMM.FRX:0000
  12.    Left            =   1020
  13.    LinkMode        =   1  'Source
  14.    LinkTopic       =   "Form1"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   4230
  18.    ScaleWidth      =   7365
  19.    Top             =   1470
  20.    Width           =   7485
  21.    WindowState     =   1  'Minimized
  22.    Begin CommandButton Command_Send 
  23.       Caption         =   "Send Text"
  24.       Height          =   1215
  25.       Left            =   1080
  26.       TabIndex        =   0
  27.       Top             =   2160
  28.       Width           =   1695
  29.    End
  30.    Begin Timer Timer_ClearStatusMessage 
  31.       Left            =   120
  32.       Top             =   600
  33.    End
  34.    Begin TextBox Text_Display 
  35.       BackColor       =   &H00C0C0C0&
  36.       FontBold        =   -1  'True
  37.       FontItalic      =   0   'False
  38.       FontName        =   "Courier"
  39.       FontSize        =   9.75
  40.       FontStrikethru  =   0   'False
  41.       FontUnderline   =   0   'False
  42.       Height          =   1580
  43.       Left            =   1080
  44.       MultiLine       =   -1  'True
  45.       TabIndex        =   1
  46.       Top             =   160
  47.       Width           =   5175
  48.    End
  49.    Begin Timer Timer_CheckReceiveBuffer 
  50.       Left            =   120
  51.       Top             =   120
  52.    End
  53.    Begin Label Label_StatusBar 
  54.       BackColor       =   &H00C0C0C0&
  55.       ForeColor       =   &H00000000&
  56.       Height          =   620
  57.       Left            =   360
  58.       TabIndex        =   2
  59.       Top             =   3520
  60.       Width           =   6375
  61.    End
  62. End
  63. '*************************************************
  64. '* GENERAL DECLARATIONS section of Form1
  65. '*************************************************
  66. DefInt A-Z
  67.  
  68. Dim DCB As CommStateDCB     'COM Device Control Block (DCB) record structure variable
  69.                 'This is a parameter needed by the Windows SetCommState API
  70.                 'function.
  71.                 'Refer to Form_Load event procedure for an example of
  72.                 'how initialize the COM DCB
  73.  
  74. Dim CommStat As COMSTAT     'COM status variable.
  75.                 'This is a parameter needed by the Windows GetCommError API
  76.                 'function
  77.  
  78. Dim ComID                   'Identifies the COM port that was opened.
  79.                 'Used by or returned by the Windows API functions
  80.                 'OpenComm, GetCommState, SetCommEventMask, GetCommEventMask,
  81.                 'ReadComm, WriteComm, FlushComm, CloseComm
  82.  
  83. '**************************************************************
  84. '* This event procedure demonstrates how to call the Windows
  85. '* API function WriteComm to send data out the COM port.
  86. '*
  87. '* Click event procedure for the command button
  88. '* (CtlName: Command_Send) that causes the contents
  89. '* of the text box (CtlName: Text_Display) to be sent
  90. '* out the COM port.
  91. '*
  92. '* Status information is displayed within caption of a
  93. '* label (CtlName: Label_StatusBar).
  94. '***************************************************************
  95. Sub Command_Send_Click ()
  96.     
  97.     'Get the data to be sent from the text box.  Note: All
  98.     'of the text contained in the text box is sent.
  99.     buffer$ = Text_Display.text
  100.  
  101.     'Send the contents of the output buffer out the COM port
  102.     r = WriteComm(ComID, buffer$, Len(buffer$))
  103.     
  104.     'Display any communications errors that might have occurred
  105.     'when attempting to write to the COM port
  106.     Call ProcessCommError
  107.     
  108. End Sub
  109.  
  110. '****************************************************************************
  111. '* Form_Load event procedure for Form1
  112. '*
  113. '* This is starting point of the program.
  114. '*
  115. '* Create the following controls with the
  116. '* following CtlNames on Form1:
  117. '*
  118. '* Control
  119. '* (Default Name)     CtlName                  Notes
  120. '* --------------     -------                  ---------------------------------
  121. '*   Text1            Text_Display             Set the MultiLine property to True
  122. '*   Command1         Command_Send             Set caption property to "Send Text"
  123. '*   Timer1           Timer_CheckReceiveBuffer
  124. '*   Timer2           Timer_ClearStatusMessage
  125. '*   Label1           Label_StatusBar
  126. '*
  127. '* This event procedure demonstrates how to call the Windows API functions,
  128. '* OpenComm and SetCommState to open the COM port.  In this example, the
  129. '* COM port is opened as the following equivalent QuickBASIC OPEN COM string:
  130. '*
  131. '*     "COM1:1200,N,8,1,DS0,CS0,CD0,RS,TB2048,RB2048"
  132. '*
  133. '******************************************************************************
  134. Sub Form_Load ()
  135.     
  136.     'Move the COM status window to the bottom of the form
  137.     Label_StatusBar.Move 0, Label_StatusBar.Top, Form1.ScaleWidth
  138.     
  139.     Form1.Show
  140.     
  141.     'Show a status message indicating that the COM port is being opened
  142.     Call ShowStatus("Opening COM1 ...")
  143.  
  144.     Do
  145.  
  146.     'Open COM1 with a 2K input and output buffer
  147.     ComID = OpenComm("COM2", 2048, 2048)
  148.     
  149.     If ComID < 0 Then
  150.     
  151.         Call ShowOpenCommError(ComID)
  152.     
  153.         If ComID = IE_OPEN Then
  154.     
  155.         m$ = "COM device already opened" + Chr$(13) + Chr$(13)
  156.         m$ = m$ + "Do you wish to use it anyway"
  157.         Response = MsgBox(m$, 36, "Communications Error")
  158.     
  159.         'Close the com port if the user selected Yes from the message box
  160.         If Response = 6 Then
  161.     
  162.             'Close the COM port if the user decided to use it anyway
  163.             r = CloseComm(Asc(DCB.Id))
  164.  
  165.         Else
  166.             
  167.             'Display a message and terminate the program
  168.             'if the user decided not to use the COM port
  169.             'that is currently open
  170.  
  171.             m$ = "Terminating application"
  172.             MsgBox m$, 16, "Communications Abort"
  173.  
  174.             End
  175.  
  176.         End If
  177.     
  178.         Else
  179.     
  180.         'Display a critical error message and terminate the program
  181.         m$ = "Error occurred attempting to open the COM port."
  182.         m$ = m$ + "  Check connection, settings and rerun the program"
  183.         MsgBox m$, 16, "Communications Error"
  184.         End
  185.     
  186.         End If
  187.     
  188.     Else
  189.     
  190.         'Set line settings for the COM port as 1200:N,8,1,CD0,CS0,DS0,RS,TB2048,RB2048
  191.         '
  192.         'The following parameter settings represent the default settings set by calling
  193.         'BuildCommDCB in the Form_Load event procedure.
  194.         '
  195.         'Set parameters as 1200: N,8,1
  196.         DCB.Id = Chr$(ComID)
  197.         DCB.BaudRate = 1200             'Other possible values include 300, 2400, 4800, 9600, 19200
  198.         DCB.ByteSize = Chr$(8)          'Other possible values include 4,5,6,7
  199.         DCB.Parity = Chr$(NOPARITY)     'Other possible values include EVENPARITY, MARKPARITY, ODDPARITY, SPACEPARITY
  200.         DCB.StopBits = Chr$(ONESTOPBIT) 'Other possible values include ONE5STOPBITS, TWOSTOPBITS
  201.                     
  202.         'Set timeout period for CD, CS and DS handshake lines respectively.  Values
  203.         'represent milliseconds.  A value of zero represents an infinite wait effectively
  204.         'disabling handshaking on that line.  Possible values can range from 0 to 65,535
  205.         'for an unsiged integer or -32,768 to 32,767 for signed integers.
  206.         '
  207.         DCB.RlsTimeOut = 0         'Carrier detect or receive-line-signal-detect (CD or RLSD) line (CD0)
  208.         DCB.CtsTimeOut = 0         'Clear-to-send (CTS) line (CS0)
  209.         DCB.DsrTimeOut = 0         'Data-set-ready (DSR) line (DS0)
  210.     
  211.         
  212.         'The following bit flags are combined in the ModeControl field below.  Because
  213.         'the following are bit fields they cannot be represented as a field of a Type ... End Type
  214.         'structure
  215.         
  216.         'DCB.fBinary = 1            Specify binary mode.  Setting this to zero causes an
  217.         '                           EOF character (Chr$(26)) to signal the end of data.
  218.         'DCB.fRtsDisabled = 1       Disable request-to-send line (RS).  A zero value enables
  219.         '                           the request-to-send line
  220.         'DCB.fParity = 0            Disable parity checking.  A value of 1 enables parity checking
  221.         'DCB.fOutCtsFlow = 0        Disable checking of clear-to-send line for output flow co